package cn.kgc.controller;


import cn.kgc.pojo.SmbmsBill;
import cn.kgc.service.BillService;
import cn.kgc.utils.PageInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("bill")
public class BillController {
    private Logger logger = Logger.getLogger(BillController.class);
    @Resource
    BillService billService;

    @RequestMapping("/limitb")
    public String limit(@RequestParam(value = "currentPageNo",required = false) Integer currentPageNo,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                        @RequestParam(value = "isPayment",required = false) Integer isPayment,
                        @RequestParam(value = "productName",required = false) String productName,
                        Model model){
        PageInfo pageInfo1 = new PageInfo();
        if (currentPageNo != null) {
            pageInfo1.setCurrentPageNo(currentPageNo);
        }
        if (pageSize != null){
            pageInfo1.setPageSize(pageSize);
        }
        int totalConut = billService.getBillConut(productName,isPayment);
        pageInfo1.setTotalConut(totalConut);
        List<SmbmsBill> list = billService.getBillByPage(pageInfo1.getCurrentPageNo(),pageInfo1.getPageSize(),productName,isPayment);
        pageInfo1.setData(list);
        model.addAttribute("page1",pageInfo1);
        model.addAttribute("productName",productName);
        model.addAttribute("isPayment",isPayment);
        return "indexBill";
    }

    @RequestMapping("/toAddBill")
    public String toAddBill(){
        return "billAdd";
    }
    @RequestMapping("/addBill")
    public String addBill(SmbmsBill smbmsBill){
        boolean flag = billService.addBill(smbmsBill);
        if (flag){
            return "redirect:/bill/limitb";
        }else {
            return "billAdd";
        }
    }

    @RequestMapping("/toDelBill")
    public String toDelBill(@RequestParam(value = "id",required = false) Integer id){
        boolean flag = billService.delBillId(id);
        if (flag){
            return "redirect:/bill/limitb";
        }else {
            return "indexBill";
        }

    }

    @RequestMapping("/toBillUpdate")
    public String toBillUpdate(@RequestParam("id")Integer id,Model model){
        SmbmsBill smbmsBill = billService.getBillId(id);
        model.addAttribute("bill",smbmsBill);
        return "billUpdate";
    }
    @RequestMapping(value = "/updateBill",method = RequestMethod.POST)
    public String update(SmbmsBill smbmsBill){
        boolean flag = billService.updateBill(smbmsBill);
        if (flag){
            return "redirect:/bill/limitb";
        }else {
            return "billUpdate";
        }
    }

    @RequestMapping("/toBillView")
    public String toBillView(@RequestParam("id")Integer id,Model model){
        SmbmsBill smbmsBill = billService.getBillId(id);
        model.addAttribute("bill",smbmsBill);
        return "billView";
    }

    @RequestMapping("/billIsExist/{billCode}")
    @ResponseBody
    public JSONObject userIsExist(@PathVariable String billCode){

        logger.info("billCode======="+billCode);
        SmbmsBill smbmsBill = billService.getBillCode(billCode);
        JSONObject jsonObject = new JSONObject();
        if (smbmsBill == null){
            jsonObject.put("code","10000");
            jsonObject.put("msg","可以使用");
        }else {
            jsonObject.put("code","10001");
            jsonObject.put("msg","不可以使用");
        }
        return jsonObject;
    }
}
